package com.ruoyi.system.outto.goods.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.V2.gameAttr.mapper.GameAttrTemplateMapper;
import com.ruoyi.system.V2.gameAttr.mapper.GameAttributeMapper;
import com.ruoyi.system.V2.goodInfo.mapper.GoodsInfoImgMapper;
import com.ruoyi.system.V2.inter.mapper.ArtRecMapper;
import com.ruoyi.system.interAttr.domain.InterAttr;
import com.ruoyi.system.interAttr.mapper.InterAttrMapper;
import com.ruoyi.system.model.domain.*;
import com.ruoyi.system.model.dto.GoodsInfoAndAttrValueDto;
import com.ruoyi.system.outto.core.LCConstant;
import com.ruoyi.system.outto.core.MapUntils;
import com.ruoyi.system.outto.goods.NEWMAIGEHAOAPI;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lz
 * @version 1.0
 * @date 2022/6/5 16:19
 */
@Slf4j
@Service
public class NEWMAIGEHAOAPIImpl implements NEWMAIGEHAOAPI {
    @Resource
    private GoodsInfoImgMapper goodsInfoImgMapper;
    @Resource
    private GameAttributeMapper gameAttributeMapper;
    @Resource
    private ArtRecMapper artRecMapper;
    @Value("${desc.title}")
    private String descTitle;
    private SalePartner salePartner;
    @Resource
    private GameAttrTemplateMapper gameAttrTemplateMapper;
    @Resource
    InterAttrMapper interAttrMapper;

    @Override
    public AjaxResult addGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        this.salePartner = salePartner;
        Map<String, Object> params = params(goodsInfoAndAttrValueDto, "add",salePartner);
        Map<String, Object> result = new HashMap<>();
        String response = HttpUtils.doPostForm("http://merchant-api.maigehao.com/api/open/goods/operate", params,null);
        String string = (MapUntils.unicodeToString(response));
        log.info("买个号添加接口请求参数={}", JSONObject.toJSONString(params));
        log.info("买个号添加接口返回参数={}", MapUntils.unicodeToString(response));
        JSONObject resultJson = JSONObject.parseObject(string);
        if(!Objects.isNull(resultJson)) {
            if (!resultJson.getString("message").contains("成功")) {
                result.put("code" , 500);
            } else {
                result.put("code" , 200);
                String data = resultJson.getString("data");
                String goods_id = JSONObject.parseObject(data).getString("goods_id");
                result.put("outId", goods_id);
                goodsInfoAndAttrValueDto.setOutId(goods_id);
                upper(goodsInfoAndAttrValueDto, salePartner);
            }
            result.put("msg", resultJson.getString("message"));
        }else{
            result.put("code" , 500);
            result.put("msg", "添加失败");
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        return AjaxResult.success(result);
    }

    //上架
    public AjaxResult upper(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        JSONObject action = new JSONObject();
        JSONObject params = new JSONObject();
        action.put("appid",salePartner.getOther());
//        ArtRec artRecreq = new ArtRec();
//        artRecreq.setConfig(salePartner.getId());
//        artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
//        List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
        action.put("goods_id",goodsInfoAndAttrValueDto.getOutId());
        params.put("data",action);
        String sign = DigestUtils.md5Hex(action+salePartner.getSaleKey());
        params.put("sign",sign);
        String response = HttpUtils.doPostForm("http://merchant-api.maigehao.com/api/open/goods/upper", params,null);
        Map<String, Object> result = new HashMap<>();
        String string = (MapUntils.unicodeToString(response));
        log.info("买个号上架接口请求参数={}", JSONObject.toJSONString(params));
        log.info("买个号上架接口返回参数={}", MapUntils.unicodeToString(response));
        JSONObject resultJson = JSONObject.parseObject(string);
        if(!Objects.isNull(resultJson)) {
            if (!resultJson.getString("message").contains("成功")) {
                result.put("code" , 500);
            } else {
                result.put("code" , 200);
            }
            result.put("msg", resultJson.getString("message"));
        }else{
            result.put("code" , 500);
            result.put("msg", "上架失败");
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        return AjaxResult.success(result);
    }
    //下架
    public AjaxResult under(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner){
        JSONObject action = new JSONObject();
        JSONObject params = new JSONObject();
        action.put("appid",salePartner.getOther());
        ArtRec artRecreq = new ArtRec();
        artRecreq.setConfig(salePartner.getId());
        artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
        List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
        action.put("goods_id",artRecs.get(0).getOutid());
        params.put("data",action);
        String sign = DigestUtils.md5Hex(action+salePartner.getSaleKey());
        params.put("sign",sign);
        String response = HttpUtils.doPostForm("http://merchant-api.maigehao.com/api/open/goods/under", params,null);
        Map<String, Object> result = new HashMap<>();
        String string = (MapUntils.unicodeToString(response));
        log.info("买个号下架接口请求参数={}", JSONObject.toJSONString(params));
        log.info("买个号下架接口返回参数={}", MapUntils.unicodeToString(response));
        JSONObject resultJson = JSONObject.parseObject(string);
        if(!Objects.isNull(resultJson)) {
            if (!resultJson.getString("message").contains("成功")) {
                result.put("code" , 500);
            } else {
                result.put("code" , 200);
            }
            result.put("msg", resultJson.getString("message"));
        }else{
            result.put("code" , 500);
            result.put("msg", "下架失败");
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        return AjaxResult.success(result);
    }

    @Override
    public AjaxResult editGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        JSONObject action = new JSONObject();
        JSONObject params = new JSONObject();
        JSONObject result = new JSONObject();
        action.put("appid",salePartner.getOther());
        ArtRec artRecreq = new ArtRec();
        artRecreq.setConfig(salePartner.getId());
        artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
        List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
        action.put("goods_id",artRecs.get(0).getOutid());
        action.put("price",goodsInfoAndAttrValueDto.getPrice());
        params.put("data",action);
        String sign = DigestUtils.md5Hex(action+salePartner.getSaleKey());
        params.put("sign",sign);
        String response = HttpUtils.doPostForm("http://merchant-api.maigehao.com/api/open/goods/resetPrice", params,null);
        log.info("买个号改价接口请求参数={}", JSONObject.toJSONString(params));
        log.info("买个号改价接口返回参数={}", MapUntils.unicodeToString(response));
        String string = (MapUntils.unicodeToString(response));
        JSONObject resultJson = JSONObject.parseObject(string);
        if(!Objects.isNull(resultJson)) {
            if (!resultJson.getString("message").contains("成功")) {
                result.put("code" , 500);
            } else {
                result.put("code" , 200);
            }
            result.put("msg", resultJson.getString("message"));
        }else{
            result.put("code" , 500);
            result.put("msg", "改价失败");
        }
        result.put("req", JSONObject.toJSONString(params));
        result.put("res", response);
        return AjaxResult.success(result);
    }


    private Map<String, Object> params(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, String type, SalePartner salePartner) {
        JSONObject action = new JSONObject();
        action.put("appid",salePartner.getOther());
        action.put("third_goods_id",String.valueOf(goodsInfoAndAttrValueDto.getId()));
        if(type.equals("add")){
            action.put("goods_id",0);
        }else{
            action.put("goods_id",1);
        }
        action.put("game_account", String.valueOf(System.currentTimeMillis()).substring(3));
        if(goodsInfoAndAttrValueDto.getGameName().equals("原神")) {
            action.put("game_password", StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getAccountPassword())?goodsInfoAndAttrValueDto.getAccountPassword():"123456");
            action.put("game_password_confirm", StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getAccountPassword())?goodsInfoAndAttrValueDto.getAccountPassword():"123456");
        }else{
            action.put("game_password", "123456789");
            action.put("game_password_confirm", "123456789");
        }
        action.put("price",goodsInfoAndAttrValueDto.getPrice());

        String substring = salePartner.getSaleName().substring(salePartner.getSaleName().indexOf("_")+1);
        InterAttr interAttr = interAttrMapper.selectInterAttrByConfig(substring, goodsInfoAndAttrValueDto.getGameId());
        action.put("game_id", interAttr.getInterGameId());

        Property(goodsInfoAndAttrValueDto,action,interAttr);



        GoodsInfoImg goodsInfoImg = new GoodsInfoImg();
        goodsInfoImg.setGoodsId(goodsInfoAndAttrValueDto.getId());
        goodsInfoImg.setType(Constants.STR_1);
        List<GoodsInfoImg> infoImgs = goodsInfoImgMapper.selectGoodsInfoImgList(goodsInfoImg);
        List<String> list = new ArrayList<>();
        if (CollectionUtils.isEmpty(infoImgs)){
            list.add(goodsInfoAndAttrValueDto.getTitleImg()==null? LCConstant.defaultImg:goodsInfoAndAttrValueDto.getTitleImg());
        }else {
            for(GoodsInfoImg infoImg1 : infoImgs){
                if(StringUtils.isNotEmpty(infoImg1.getImgUrl())){
                    list.add(infoImg1.getImgUrl());
                }
            }
        }
        action.put("img_list",list.size()>20?list.subList(0,19):list);
        action.put("introduction","【"+descTitle+"_"+ goodsInfoAndAttrValueDto.getId()+"】" + goodsInfoAndAttrValueDto.getDes().replace("+",""));
//        action.put("itemList",options);
        log.info("cover={}", action+salePartner.getSaleKey());
        String sign = DigestUtils.md5Hex(action+salePartner.getSaleKey());
        JSONObject params = new JSONObject();
        params.put("data",action);
        params.put("sign",sign);
        return params;
    }


    //王者荣耀参数
    private JSONObject Property(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, JSONObject action, InterAttr interAttr) {

        if(true){
            String server = interAttr.getServer();
            JSONObject jsonObject = JSONObject.parseObject(server);
            //开始匹配一级区服
            String interServeReqs = jsonObject.getString("interServeReqs");
            String qufuid = "";
            if (Objects.equals(goodsInfoAndAttrValueDto.getGameType(), "0")) {
                qufuid = goodsInfoAndAttrValueDto.getSystemId() + "" + goodsInfoAndAttrValueDto.getCarrierId() + "";

            } else {
                qufuid = goodsInfoAndAttrValueDto.getGroupId() + "";
            }
            if (Objects.equals(goodsInfoAndAttrValueDto.getGameName(), "三角洲行动")) {
                qufuid = goodsInfoAndAttrValueDto.getSystemId().toString();
            }
            List<JSONObject> oneServer = JSONObject.parseArray(interServeReqs, JSONObject.class);

            JSONObject oneServerJson = new JSONObject();
            for (JSONObject object : oneServer) {
                String ydServeId = object.getString("YdServeId");
                if (StringUtils.isNotEmpty(ydServeId)) {
                    ydServeId = ydServeId.replace(",", "").replace(" ", "");
                    if (ydServeId.contains(qufuid)) {
                        oneServerJson = object;
                        break;
                    }
                }
            }
            if (Objects.isNull(oneServerJson)) {
                throw new GlobalException("运营商未匹配到");
            }

            long game_server_id = 0;

            long game_region_id = oneServerJson.getInteger("id");

            String twoServeStr = oneServerJson.getString("interServerRes");
            List<JSONObject> twoServer = JSONObject.parseArray(twoServeStr, JSONObject.class);

            if(CollectionUtils.isNotEmpty(twoServer)){
                String serverId = goodsInfoAndAttrValueDto.getServerId().toString();
                if (Objects.equals(goodsInfoAndAttrValueDto.getGameName(), "三角洲行动")) {
                    serverId = goodsInfoAndAttrValueDto.getCarrierId().toString();
                }
                JSONObject twoServerJson = new JSONObject();
                for (JSONObject object : twoServer) {
                    String ydServeId = object.getString("YdServeId");
                    if (StringUtils.isNotEmpty(ydServeId)) {
                        ydServeId = ydServeId.replace(",", "");
                        if (Objects.equals(serverId, ydServeId)) {
                            twoServerJson = object;
                            break;
                        }
                    }
                }
                game_server_id = twoServerJson.getInteger("id");
            }


            action.put("game_server_id",game_server_id);
            action.put("game_region_id",game_region_id);

            //===========区服已匹配完成==============================//
        }

        //========================开始匹配单选属性=====================//
        String radioAttr = interAttr.getRadioAttr();
        JSONObject jsonObject = JSONObject.parseObject(radioAttr);
        String interRadioReqs = jsonObject.getString("interRadioReqs");

        System.out.println(JSONObject.toJSONString(interRadioReqs));

//        String interRadioReqs = jsonObject.getString("interServeReqs");
        List<JSONObject> jsonObjects = JSONObject.parseArray(interRadioReqs, JSONObject.class);
        GameAttrTemplate gameAttrTemplate = new GameAttrTemplate();
        Field[] declaredFields = goodsInfoAndAttrValueDto.getClass().getDeclaredFields();
        List<Map<String, Object>> propertys = new ArrayList<>();
        if (!org.springframework.util.CollectionUtils.isEmpty(jsonObjects)) {
            /**
             * 1、先查询自定义中所有单选的字段
             * 2、再查该账号单选字段中的选项
             * 3、再去接口属性匹配表过滤
             */

            gameAttrTemplate.setGameId(goodsInfoAndAttrValueDto.getGameId());
            gameAttrTemplate.setIsEnable("0");
            gameAttrTemplate.setFieldType("radio");
            List<GameAttrTemplate> gameAttrTemplates = gameAttrTemplateMapper.selectGameAttrTemplateList(gameAttrTemplate);



            List<JSONObject> defObjects = new ArrayList<>();
            defObjects.addAll(jsonObjects);
            for (JSONObject object : jsonObjects) {
                for (GameAttrTemplate attrTemplate : gameAttrTemplates) {

                    List<String> valueList = new ArrayList<>();
                    Map<String,Object> map = new HashMap<>();

                    String aavName = attrTemplate.getAavName();
                    for (Field declaredField : declaredFields) {
                        String name = declaredField.getName();
                        if (aavName.equals(name)) {
                            //一级属性
//                        List<JSONObject> oneAttr = jsonObjects.stream().filter(j -> Objects.equals(j.getLong("YdConfigId"), attrTemplate.getId())).collect(Collectors.toList());

                            //注意 YdConfigId 对应的是 f字段对应的 id
                            if (Objects.equals(object.getLong("YdConfigId"), attrTemplate.getId())) {
                                String InterConfigId = object.getString("InterConfigId");
                                defObjects.remove(object);//如果匹配上，则去掉
                                String interRadioReqs1 = object.getString("interRadioReqs");
//                            String interRadioReqs1 = object.getString("interServerRes");
                                List<JSONObject> jsonObjects2 = JSONObject.parseArray(interRadioReqs1, JSONObject.class);
                                declaredField.setAccessible(true);
                                try {
                                    //获取选择的值 例如  实名中的可二次

                                    Object o = declaredField.get(goodsInfoAndAttrValueDto);
                                    String oneValue = String.valueOf(o);
                                    List<JSONObject> twoAttr = jsonObjects2.stream().filter(j -> StringUtils.isNotEmpty(j.getString("YdConfigId"))?j.getString("YdConfigId").contains(oneValue):"133435".contains(oneValue)).collect(Collectors.toList());
                                    Boolean required = object.getBoolean("required");
                                    String interConfigName = object.getString("InterConfigName");
                                    String defaultValue = object.getString("defaultValue");
                                    if (org.springframework.util.CollectionUtils.isEmpty(twoAttr) && required) {
                                        if (StringUtils.isNotEmpty(defaultValue)) {
                                            map.put("item_id", InterConfigId);
                                            valueList.add(defaultValue);
                                            map.put("valueList",valueList);
                                        }
                                    }
                                    if (!org.springframework.util.CollectionUtils.isEmpty(twoAttr)) {
                                        JSONObject jsonObject2 = twoAttr.get(0);
                                        String interConfigId2 = jsonObject2.getString("InterConfigId");
                                        map.put("item_id", InterConfigId);
                                        valueList.add(interConfigId2);
                                        map.put("valueList",valueList);
                                        if (CollectionUtils.isNotEmpty(valueList)) {
                                            propertys.add(map);
                                        }
                                    }
                                } catch (Exception e) {
                                    throw new GlobalException("单选项" + attrTemplate.getFieldName() + "匹配错误");
                                }
                            }
                        }
                    }
                }
            }
            //开始匹配默认值
            for (JSONObject object : defObjects) {
                HashMap<String, Object> mateMap = new HashMap<>();
                List<String> valueList = new ArrayList<>();
//            String InterConfigId = object.getString("InterConfigId");
                String InterConfigId = object.getString("id");
                String interRadioReqs1 = object.getString("interRadioReqs");
                JSONArray jsonArray = JSONArray.parseArray(interRadioReqs1);
                for(Object o : jsonArray) {
                    String defaultValue = JSONObject.parseObject(o.toString()).getString("defaultValue");

                    if (StringUtils.isNotEmpty(defaultValue)) {
//                mateMap.put("id", InterConfigId);
//                mateMap.put("val", defaultValue);

                        mateMap.put("item_id", InterConfigId);
                        valueList.add(defaultValue);
                        mateMap.put("valueList", valueList);
                        if (CollectionUtils.isNotEmpty(valueList)) {
                            propertys.add(mateMap);
                        }
                    }
                }
            }
        }


        //========================单选属性匹配结束=====================//


        //========================开始匹配多选属性=====================//
        /**
         * 1、先查询自定义中所有多选的字段
         * 2、再查该账号单选字段中的选项
         * 3、再去接口属性匹配表过滤
         */
        String checkAttr = interAttr.getCheckAttr();
        JSONObject checkAttrJson = JSONObject.parseObject(checkAttr);
        String interCheckBoxReqs = checkAttrJson.getString("interCheckBoxReqs");
//        String interCheckBoxReqs = checkAttrJson.getString("interServeReqs");
        List<JSONObject> checkJsonList = JSONObject.parseArray(interCheckBoxReqs, JSONObject.class);
        List<String> types = new ArrayList<>();
        if (!org.springframework.util.CollectionUtils.isEmpty(jsonObjects)) {

            gameAttrTemplate = new GameAttrTemplate();
            gameAttrTemplate.setGameId(goodsInfoAndAttrValueDto.getGameId());
            gameAttrTemplate.setIsEnable("0");

            types.add("checkbox");
            types.add("boxradio");
            gameAttrTemplate.setFieldTypes(types);
            List<GameAttrTemplate> checkAttrTemplates = gameAttrTemplateMapper.selectGameAttrTemplateList(gameAttrTemplate);

            for (JSONObject object : checkJsonList) {
                List<String> valueList = new ArrayList<>();
                Map<String,Object> map = new HashMap<>();
                String InterConfigId = object.getString("InterConfigId");
                for (GameAttrTemplate attrTemplate : checkAttrTemplates) {

                    String aavName = attrTemplate.getAavName();
                    for (Field declaredField : declaredFields) {
                        String name = declaredField.getName();
                        if (aavName.equals(name)) {
                            declaredField.setAccessible(true);

                            String ydConfigIdLeveOne = object.getString("YdConfigId");
                            if (StringUtils.isNotEmpty(ydConfigIdLeveOne)) {

                                String[] split2 = ydConfigIdLeveOne.split(",");
                                List<String> list2 = new ArrayList<>();
                                for (String s : split2) {
                                    s = s.replace(" ", "");
                                    list2.add(s);
                                }
                                if (list2.contains(attrTemplate.getId() + "")) {
                                    Object o = null;
                                    try {
                                        o = declaredField.get(goodsInfoAndAttrValueDto);
                                    } catch (IllegalAccessException e) {
                                        throw new GlobalException("多选项" + attrTemplate.getFieldName() + "匹配错误");
                                    }
                                    String oneValue = String.valueOf(o);
                                    //一级属性
                                    String ydConfigId = object.getString("YdConfigId");
                                    String InterConfigName = object.getString("InterConfigName");

                                    Boolean required = object.getBoolean("required");
                                    if (StringUtils.isNotEmpty(ydConfigId)) {
                                        String[] split = ydConfigId.split(",");
                                        //一级属性已经匹配，开始匹配二级属性
                                        if (Arrays.asList(split).contains(attrTemplate.getId() + "")) {
                                            String interCheckBoxReqsStr = object.getString("interCheckBoxReqs");
//                                        String interCheckBoxReqsStr = object.getString("interServerRes");
                                            //开始匹配二级属性
                                            List<JSONObject> jsonObjects2 = JSONObject.parseArray(interCheckBoxReqsStr, JSONObject.class);
                                            /**
                                             * 咱们的f字段  多选项都是用空格 隔开，用,号切割数组，跟匹配的数据匹配、匹配上说明此账号勾选的有此属性
                                             */

                                            for (JSONObject jsonObject1 : jsonObjects2) {
                                                if (StringUtils.isNotEmpty(oneValue)) {
                                                    String[] split1 = oneValue.split(",");
                                                    String ydConfigId1 = jsonObject1.getString("YdConfigId");
                                                    if(StringUtils.isNotEmpty(ydConfigId1)) {
                                                        ydConfigId1 = ydConfigId1.replace(" ", "");
                                                        for (String s : split1) {
                                                            if (Objects.equals(s, ydConfigId1)) {
                                                                String InterConfigId2 = jsonObject1.getString("InterConfigId");
                                                                valueList.add(InterConfigId2);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    } else {
                                        throw new GlobalException("属性参数错误，请匹配后重新配置");
                                    }
                                }
                            }
                        }
                    }
                }
                if (CollectionUtils.isNotEmpty(valueList)) {
                    map.put("item_id", InterConfigId);
                    map.put("valueList",valueList);
                    propertys.add(map);
                }
            }
        }

        //========================多选属性匹配完成=====================//


        //========================开始匹配输入框=====================//
        String inputAttr = interAttr.getInputAttr();
        JSONObject jsonObject1 = JSONObject.parseObject(inputAttr);
        String inputJson = jsonObject1.getString("inputJson");
        List<JSONObject> jsonObjects1 = JSONObject.parseArray(inputJson, JSONObject.class);
        gameAttrTemplate = new GameAttrTemplate();
        gameAttrTemplate.setGameId(goodsInfoAndAttrValueDto.getGameId());
        gameAttrTemplate.setIsEnable("0");
        types = new ArrayList<>();
        types.add("number");
        types.add("input");
        gameAttrTemplate.setFieldTypes(types);
        List<GameAttrTemplate> inputTemplates = gameAttrTemplateMapper.selectGameAttrTemplateList(gameAttrTemplate);
        List<JSONObject> defInputObjects = new ArrayList<>();
        defInputObjects.addAll(jsonObjects1);
        for (JSONObject object : jsonObjects1) {
            for (GameAttrTemplate inputTemplate : inputTemplates) {
//                HashMap<String, Object> mateMap = new HashMap<>();
                List<String> valueList = new ArrayList<>();
                Map<String,Object> map = new HashMap<>();

                String aavName = inputTemplate.getAavName();
                if (Objects.equals(object.getString("aavName"), inputTemplate.getAavName())) {
                    defInputObjects.remove(object);
                    String aavNameInter = object.getString("interName");
                    String interId = object.getString("interId");
                    for (Field declaredField : declaredFields) {
                        String name = declaredField.getName();
                        if (Objects.equals(aavName, name)) {
                            declaredField.setAccessible(true);
                            Object o = null;
                            try {
                                o = declaredField.get(goodsInfoAndAttrValueDto);
                            } catch (IllegalAccessException e) {
                                throw new GlobalException("3属性参数错误，请匹配后重新配置");
                            }
                            String oneValue = String.valueOf(o);
                            Boolean required = object.getBoolean("required");
                            if (required && StringUtils.isEmpty(oneValue)) {
                                throw new GlobalException(aavNameInter + "必传，但是该商品没有该值");
                            }
//                            mateMap.put("id", interId);
//                            mateMap.put("val", oneValue);
//                            propertys.add(mateMap);


                            map.put("item_id", interId);
                            valueList.add(oneValue);
                            map.put("valueList",valueList);
                            if (CollectionUtils.isNotEmpty(valueList)) {
                                propertys.add(map);
                            }
                        }
                    }
                }

            }
        }

        //开始匹配默认值
        for (JSONObject object : defInputObjects) {
//            HashMap<String, Object> mateMap = new HashMap<>();
            List<String> valueList = new ArrayList<>();
            Map<String,Object> map = new HashMap<>();
            String id = object.getString("id");
            String defaultValue = object.getString("defaultValue");
            if (StringUtils.isNotEmpty(defaultValue)) {
//                mateMap.put("id", InterConfigId);
//                mateMap.put("val", defaultValue);
//                if (Objects.nonNull(mateMap.get("val"))) {
//                    propertys.add(mateMap);
//                }

                map.put("item_id", id);
                valueList.add("0");
                map.put("valueList",valueList);
                if (CollectionUtils.isNotEmpty(valueList)) {
                    propertys.add(map);
                }
            }
        }

        action.put("itemList",propertys);
        return action;
    }



    //只保留中文
    private String onlyHanZi(String str) {
        String reg = "[^\u4e00-\u9fa5]";
        str = str.replaceAll(reg, "");
        return str;
    }
    private List<GameAttribute> getGameAttributs(String ids) {
        return gameAttributeMapper.selectGameAttributeListByIds((Long[]) ConvertUtils.convert(ids.split(","),
                Long.class));
    }
    private String reIds(String allIds, String ids) {
        if (StringUtils.isNotEmpty(ids)) {
            if (StringUtils.isNotEmpty(allIds)) {
                allIds = allIds + "," + ids;
            } else {
                allIds = ids;
            }
        }
        return allIds;
    }

    @Override
    public AjaxResult delGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner) {
        //下架
        AjaxResult underresult = under(goodsInfoAndAttrValueDto,salePartner);
        JSONObject action = new JSONObject();
        JSONObject params = new JSONObject();
        Map<String, Object> result = new HashMap<>();
        if(underresult.get("msg").equals("操作成功")) {
            action.put("appid", salePartner.getOther());
            ArtRec artRecreq = new ArtRec();
            artRecreq.setConfig(salePartner.getId());
            artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
            List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
            action.put("goods_id",artRecs.get(0).getOutid());
            params.put("data", action);
            String sign = DigestUtils.md5Hex(action + salePartner.getSaleKey());
            params.put("sign", sign);
            String response = HttpUtils.doPostForm("http://merchant-api.maigehao.com/api/open/goods/delete", params, null);
            String string = (MapUntils.unicodeToString(response));
            log.info("买个号删除接口请求参数={}", JSONObject.toJSONString(params));
            log.info("买个号删除接口返回参数={}", MapUntils.unicodeToString(response));
            JSONObject resultJson = JSONObject.parseObject(string);
            ArtRec artRec = artRecs.get(0);
            if (!Objects.isNull(resultJson)) {
                if (!resultJson.getString("message").contains("成功")) {
                    result.put("code", 500);
                } else {
                    result.put("code", 200);
                }
                result.put("msg", resultJson.getString("message"));
            } else {
                result.put("code", 500);
                result.put("msg", "删除失败");
            }
            result.put("req", JSONObject.toJSONString(params));
            result.put("res", response);
        }else{
            result.put("req", JSONObject.toJSONString(params));
            result.put("res", "下架失败");
        }
        return AjaxResult.success(result);
    }

    public static void main(String[] args) {
//        String response = getGameList();
        String response = getGameProperty(33);

        JSONArray jsonArray = JSONArray.parseArray(JSONObject.parseObject(response).getString("data"));
        List<String> list = new ArrayList<>();
//        for(Object o:jsonArray){
//            list.add(JSONObject.parseObject(o.toString()).getString("title"));
//        }
//        for(String s :list){
//            System.out.println(s);
//        }
        System.out.println(MapUntils.unicodeToString(response));
    }

    static String getGameList(){
        JSONObject jsonObject = new JSONObject();
        String gameServer = "http://account-api.maigehao.com/api/open/goods/gameList";
        JSONObject params = new JSONObject();
        jsonObject.put("appid","mgh1065025340218");
        String sign = DigestUtils.md5Hex(jsonObject+"57ca2639e2f074253538ea0edae858a2");
        params.put("sign",sign);
        params.put("data",jsonObject);
        String response = HttpUtils.doPostForm(gameServer, params,null);
        return response;
    }

    public static String getGameServer(Integer gameId){
        JSONObject jsonObject = new JSONObject();
        String gameServer = "http://account-api.maigehao.com/api/open/goods/gameServer";
//        String gameProperty = "http://account-api-test.maigehao.com/api/open/goods/gameProperty";
        JSONObject params = new JSONObject();
        jsonObject.put("appid","mgh1065025340218");
        jsonObject.put("game_id",gameId);
        log.info("cover={}", jsonObject+"57ca2639e2f074253538ea0edae858a2");
        String sign = DigestUtils.md5Hex(jsonObject+"57ca2639e2f074253538ea0edae858a2");
        log.info("sign={}", sign);
        params.put("sign",sign);
        params.put("data",jsonObject);
        System.out.println(params);
        String response = HttpUtils.doPostForm(gameServer, params,null);
        return response;
    }
    public static String getGameProperty(Integer gameId){
        JSONObject jsonObject = new JSONObject();
        //String gameServer = "http://account-api.maigehao.com/api/open/goods/gameServer";
        String gameProperty = "http://account-api.maigehao.com/api/open/goods/gameProperty";
        JSONObject params = new JSONObject();
        jsonObject.put("appid","mgh1065025340218");
        jsonObject.put("game_id",gameId);
        log.info("cover={}", jsonObject+"57ca2639e2f074253538ea0edae858a2");
        String sign = DigestUtils.md5Hex(jsonObject+"57ca2639e2f074253538ea0edae858a2");
        log.info("sign={}", sign);
        params.put("sign",sign);
        params.put("data",jsonObject);
        System.out.println(params);
        String response = HttpUtils.doPostForm(gameProperty, params,null);
        return response;
    }
}
